package com.cimr.comm.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.cimr.comm.Constants;
import com.cimr.comm.dto.HttpResult;
import com.cimr.comm.exception.NotLoginException;
import com.cimr.comm.token.TokenUtil;
import com.cimr.comm.token.UserToken;
import com.cimr.comm.util.ConfUtil;
import com.cimr.comm.util.ShiroUtil;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by liqi on 2017/8/3.
 */
@Controller
@RequestMapping("/ajax")
public class LoginAjaxController {
    private static final Log log = LogFactory.get();

    @RequestMapping({"login"})
    @ResponseBody
    public HttpResult login (@RequestParam(value = "username", required = true) String username,
                             @RequestParam(value = "pswd", required = false) String pswd,
                             HttpServletRequest request, ModelMap data) throws NotLoginException {
        UserToken token = new UserToken(username, pswd);
        Subject subject = ShiroUtil.getSubject();
        try {
            subject.login(token);
            SavedRequest savedRequest = WebUtils.getSavedRequest(request);
            String url = "";
            if (savedRequest==null || savedRequest.getRequestUrl()==null || savedRequest.getRequestUrl().equalsIgnoreCase(Constants.LOGIN_AJAX_URL)) {
                url =  ConfUtil.getConf("main.indexPage");
            } else {
                // 跳转到登录前的页面
                String contextPath = request.getContextPath();
                url = StrUtil.removePrefix(savedRequest.getRequestUrl(), contextPath);
            }
            data.put("url", url);

            HttpResult result = new HttpResult(true, "");
            result.setData(data);
            return result;
        } catch (Exception e) {
            throw new NotLoginException("用户名或密码错误。");
//            HttpResult result = new HttpResult(false, "用户名或密码错误！");
//            data.addAttribute(result);
//            return result;
        }

    }

    /**
     * 退出
     * @return
     */
    @RequestMapping({"logout"})
    @ResponseBody
    public HttpResult logout(){
        HttpResult result = new HttpResult(true, "");
        try {
//            log.info("=================");
//            HttpUtil.post("http://127.0.0.1:8060/logout_demo", "");
            TokenUtil.logout();
        } catch (Exception e) {
            result.setSuccess(false);
            result.setError("退出出错!");
            log.error(e, "退出出现错误，{}",e.getMessage());
        }
        return result;
    }

}
